using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;

public class Stats : CoreComponent
{
    [field: SerializeField] public Stat Health { get; private set; }
    [field: SerializeField] public Stat Poise { get; private set; }

    [SerializeField] private float poiseRecoveryRate;

    protected override void Awake()
    {
        base.Awake();
        EventManager.Register<UnolockSkillEvent>(IncreaseHealth);
    }

    private void Update()
    {
        if (Poise.CurrentValue.Equals(Poise.MaxValue)) return;
        Poise.Increase(poiseRecoveryRate * Time.deltaTime);
    }

    /// <summary>
    ///     ���ڲ���ÿ�����ɵ��˵����˶���أ����Ե�����������е��˱���һ����ڵ�������Awakeֻ���ڼ���ʱ����һ�����Ի���ֿ�Ѫ����
    /// </summary>
    private void OnEnable()
    {
        Health.Init();
        Poise.Init();
    }

    private void IncreaseHealth(UnolockSkillEvent evt)
    {
        var data = evt.skill.upgradeData;
        if (core.transform.parent.gameObject.name == "Player")
            foreach (var item in data)
                if (item.statTypes == StatTypes.Health)
                {
                    if (item.isPercentage)
                    {
                        Health.MaxValue = (float)(Health.MaxValue * (1 + item.skillIncreaseAmount * 0.01));
                        Health.CurrentValue = (float)(Health.CurrentValue * (1 + item.skillIncreaseAmount * 0.01));
                    }
                    else
                    {
                        Health.MaxValue += item.skillIncreaseAmount;
                        Health.CurrentValue += item.skillIncreaseAmount;
                    }
                }
    }
}